package com.dd.stack.kafka.producer.controller;

import com.dd.stack.common.util.response.ApiResponse;
import com.dd.stack.kafka.common.pojo.BO.JsonUserBO;
import com.dd.stack.kafka.common.pojo.BO.User;
import com.dd.stack.kafka.common.pojo.convertor.JsonUserConvertor;
import com.dd.stack.kafka.common.pojo.req.ProducerSendJsonUserReq;
import com.dd.stack.kafka.producer.service.inner.impl.KafkaServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/2/14 2:41
 * @Description KafkaController
 */
@Slf4j
@RestController
@Api(tags = "Kafka基本操作")
@RequiredArgsConstructor
@RequestMapping("/kafka")
public class KafkaController {

    @Autowired
    private KafkaServiceImpl kafkaService;

    @PostMapping("/send/msg")
    @ApiOperation("接收客户端消息并发送到Kafka")
    public ApiResponse<String> sendMessage(@ApiParam("要发送的消息（String）") @RequestBody String message) {
        kafkaService.sendMessage(message);
        return ApiResponse.success("Message sent successfully");
    }

    //@PostMapping("/send/user")
    //@ApiOperation("接收客户端消息并发送到Kafka")
    //public ApiResponse<String> sendUser(@ApiParam("要发送的消息（User对象）") @RequestBody User user) {
    //    kafkaService.sendUser(user);
    //    return ApiResponse.success("Message sent successfully");
    //}

    @PostMapping("/send/json-user")
    @ApiOperation("接收客户端消息并发送到Kafka(使用Json序列化)")
    public ApiResponse<String> sendJsonUser(@ApiParam("要发送的消息（User对象）") @RequestBody ProducerSendJsonUserReq req) {
        kafkaService.sendJsonUser(JsonUserConvertor.convert2JsonUserBO(req));
        return ApiResponse.success("Message sent successfully");
    }
}
